﻿using System.Collections.Generic;
using ExcelLibrary.SpreadSheet;
using HappyBa.Core.Data;
using HappyBa.Core.Validators;
using HappyBa.Localization;

namespace HappyBa.Core.Reporting
{
    class ExcelValidationReportGenerator : IValidationReportGenerator
    {
        private readonly IReportFilePathGetter reportFilePathGetter;

        public ExcelValidationReportGenerator(IReportFilePathGetter reportFilePathGetter)
        {
            this.reportFilePathGetter = reportFilePathGetter;
        }

        public void Generate(IList<ValidationResult> results)
        {
            reportFilePathGetter.Build(x => Create(x, results));
        }

        private static void Create(string fileName, IEnumerable<ValidationResult> results)
        {
            var workbook = new Workbook();
            var worksheet = new Worksheet(HappyBaLang.ExcelReport);
            worksheet.Cells[0, 0] = new Cell(HappyBaLang.Type);
            worksheet.Cells[0, 1] = new Cell(HappyBaLang.Validator);
            worksheet.Cells[0, 2] = new Cell(HappyBaLang.Task);
            worksheet.Cells[0, 3] = new Cell(HappyBaLang.User);
            worksheet.Cells[0, 4] = new Cell(HappyBaLang.Message);

            var lineNo = 1;
            foreach (var result in results)
            {
                worksheet.Cells[lineNo, 0] = worksheet.BuildCell(result.Type == ValidationResultType.Error? "E":"W");
                worksheet.Cells[lineNo, 1] = worksheet.BuildCell(result.ValidatorName);
                worksheet.Cells[lineNo, 2] = worksheet.BuildCell(result.Entity);
                worksheet.Cells[lineNo, 3] = worksheet.BuildCell(result.User);
                worksheet.Cells[lineNo, 4] = worksheet.BuildCell(result.Message);
                ++lineNo;
            }
            workbook.SaveResults(fileName, lineNo, worksheet);
        }
    }
}
